﻿using System;
using System.Collections.Generic;

namespace Soul.XLS
{
    public class ExcelTable
    {
        public string Name { get; set; }
        public ExcelRowCollection Rows { get; }
        public ExcelColumnCollection Headers { get; private set; } = new ExcelColumnCollection();
        public ExcelColumnCollection Columns { get; private set; } = new ExcelColumnCollection();
        internal ExcelFooterCollection Footers { get; private set; } = new ExcelFooterCollection();
        internal ExcelRegion FreezeRegion { get; set; }
        internal List<ExcelRegion> GroupRows { get; } 
        public ExcelMergeRegionCollection MergeRegions { get; }

        public ExcelTable()
        {
            Rows = new ExcelRowCollection();
            GroupRows = new List<ExcelRegion>();
            MergeRegions = new ExcelMergeRegionCollection();
        }

        public ExcelTable(string name) : this()
        {
            Name = name;
        }

        public void ApplyHeaders(ExcelColumnCollection headers)
        {
            Headers = headers;
            Columns = headers.Columns;
        }

        public ExcelRow NewRow()
        {
            return new ExcelRow(this);
        }


        public void AddRow(Action<ExcelRow> row)
        {
            var newRow = NewRow();
            row(newRow);
            Rows.Add(newRow);
        }

        public void AddFooter(Action<ExcelFooter> footer)
        {
            var newFooter = new ExcelFooter(this);
            footer(newFooter);
            Footers.Add(newFooter);
        }

        public void FreezePanes(int row, int column)
        {
            FreezeRegion = new ExcelRegion(row, column);
        }

        public void GroupByRows(int startRow, int endRow)
        {
            GroupRows.Add(new ExcelRegion(startRow, endRow));
        }
    }
}
